package com.czy.owner.ui.order.frament;

import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Toast;

import com.czy.owner.R;
import com.czy.owner.adapter.SelectCardAdapter;
import com.czy.owner.api.OrderOwnerPackageSelectApi;
import com.czy.owner.callback.RecycleViewDivider;
import com.czy.owner.callback.RecyclerHeadersDecoration;
import com.czy.owner.db.UserHelper;
import com.czy.owner.entity.CarInfo;
import com.czy.owner.entity.PackageDetailsModel;
import com.czy.owner.entity.PackageSelectModel;
import com.czy.owner.entity.SelectBespokeListModel;
import com.czy.owner.global.Constants;
import com.czy.owner.net.http.HttpManager;
import com.czy.owner.net.listener.HttpOnNextListener;
import com.czy.owner.ui.BaseFragment;
import com.czy.owner.utils.PhoneUtils;
import com.czy.owner.utils.SharedPreferencesUtils;
import com.easyrecycleview.EasyRecyclerView;
import com.easyrecycleview.adapter.RecyclerArrayAdapter;
import com.trello.rxlifecycle.components.support.RxAppCompatActivity;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;

/**
 * Created by hedepu on 2016/8/28 .
 * 套餐选择
 */
public class SelectPackageFragment extends BaseFragment {
    @BindView(R.id.recyclerView)
    EasyRecyclerView recyclerView;
    private SelectCardAdapter adapter;
    private int page = 1;
    private Handler handler = new Handler();
    private String session = "";
    private List<PackageSelectModel> dataList = new ArrayList<>();
    public static String unPaidDepositTime = "";

    private List<PackageSelectModel> selectedList = new ArrayList<>();
    private String packageNum = "0";

    @Override
    protected int attachLayoutRes() {
        return R.layout.fragment_base_subcribeorder;
    }

    @Override
    protected void initInjector() {
        if (getActivity().getIntent().getSerializableExtra("package") != null)
            selectedList = (List<PackageSelectModel>) getActivity().getIntent().getSerializableExtra("package");
    }

    @Override
    protected void initViews() {
        init();
        getOwnerCarData();
    }


    @Override
    protected void updateViews() {

//        onRefresh();
    }

    public List<PackageSelectModel> getSelectList() {
//        selectedList.clear();
//        for (PackageSelectModel packageSelectModel : dataList) {
//            if (packageSelectModel.getPackageItemListBean().getCurrentCount() != 0) {
//                selectedList.add(packageSelectModel);
//            }
//        }

        return adapter.getSelectedList();
    }

    public void selectStore() {
        dataList.clear();
        selectedList.clear();
        adapter.setSelectedList(selectedList);
        adapter.setList(dataList);
        adapter.notifyDataSetChanged();
        recyclerView.showProgress();
        getOwnerCarData();
    }

    private void init() {
        session = UserHelper.getInstance().getSessionInfoModel(getActivity()).getSession();
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        recyclerView.setAdapterWithProgress(adapter = new SelectCardAdapter(getActivity(), dataList));
        recyclerView.setEmptyView(R.layout.view_empty_common);
        recyclerView.setErrorView(R.layout.view_error_refresh);
        recyclerView.getSwipeToRefresh().setRefreshing(false);
        final RecyclerHeadersDecoration headersDecor = new RecyclerHeadersDecoration(adapter);
        recyclerView.addItemDecoration(headersDecor);
        recyclerView.addItemDecoration(new RecycleViewDivider(getActivity(), LinearLayoutManager.VERTICAL, R.drawable.shape_recyclerview_divider_decoration));
        adapter.setSelectedList(selectedList);
        adapter.registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() {
            @Override
            public void onChanged() {
                headersDecor.invalidateHeaders();
            }
        });
        recyclerView.getErrorView().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getOwnerCarData();
                recyclerView.showProgress();
            }
        });

    }

    private void getOwnerCarData() {
        OrderOwnerPackageSelectApi orderOwnerCarsApi = new OrderOwnerPackageSelectApi(new HttpOnNextListener<List<PackageDetailsModel>>() {
            @Override
            public void onNext(List<PackageDetailsModel> list) {
                setDataList(list);
            }

            @Override
            public void onCacheNext(String cache) {
                super.onCacheNext(cache);
            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);
                PhoneUtils.ShowToastMessage(getActivity(), "获取套餐信息失败");
                recyclerView.showError();
            }
        }, (RxAppCompatActivity) getActivity());
        orderOwnerCarsApi.setSession(UserHelper.getInstance().getSessionInfoModel(getActivity()).getSession());
        orderOwnerCarsApi.setStoreId(Integer.parseInt(SharedPreferencesUtils.getParam(getActivity(), UserHelper.getInstance().getUserAccount() + Constants.SHARED_PREFERENCES_KEY_BINDSTOREID, 0) + ""));
        HttpManager httpManager = HttpManager.getInstance();
        httpManager.doHttpDeal(orderOwnerCarsApi);
    }

    /**
     *
     * **/
    public void setDataList(List<PackageDetailsModel> list) {
        packageNum = list.size() + "";
        for (int i = 0; i < list.size(); i++) {
            for (int j = 0; j < list.get(i).getPackageItemList().size(); j++) {
                PackageSelectModel packageSelectModel = new PackageSelectModel();
                packageSelectModel.setPackageItemListBean(list.get(i).getPackageItemList().get(j));
                packageSelectModel.setDes(new Long((long) i));
                packageSelectModel.setPackageName(list.get(i).getPackageName());
                packageSelectModel.setEndTime(list.get(i).getEndTime());
                packageSelectModel.setStartTime(list.get(i).getStartTime());
                packageSelectModel.setUserOrderId(list.get(i).getUserOrderId());
                packageSelectModel.setUserPackageId(list.get(i).getUserPackageId());
                dataList.add(packageSelectModel);

            }
        }

        adapter.setList(dataList);


    }

    public String getPackageNum() {
        return packageNum;
    }

    @Override
    public void reloadData() {
    }
}